package com.jinkaimen.kqb.ui.activity.hss;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Button;
import android.widget.FrameLayout;

import com.ht.baselibs.ui.base.BaseActivity;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.ui.fragment.centraldocument.MarketingFragment;
import com.jinkaimen.kqb.ui.fragment.centraldocument.MaterielDownFragment;
import com.jinkaimen.kqb.ui.weiget.SegmentButton;

/**
 * Created by lixuebo on 17/7/19.
 */

public class CentralDocumentActivity extends BaseActivity {

    private SegmentButton segmentButton;
    private FrameLayout frameLayout;
    private FragmentManager fragmentManager;

    private Fragment marketingFragment;
    private Fragment materielDownFragment;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("中央文案库");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_central_document;
    }

    @Override
    protected void initChildView() {

        segmentButton = (SegmentButton) findViewById(R.id.sb_central_document_activity);
        frameLayout = (FrameLayout) findViewById(R.id.fl_central_document_activity);

    }

    @Override
    protected void initChildData() {
        fragmentManager = getSupportFragmentManager();
        setTabSelection(1);
    }

    @Override
    protected void setChildViewListener() {
        segmentButton.setOnCheckedChangeListener(new SegmentButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, Button button) {
                setTabSelection(position + 1);
            }

        });
    }

    private void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        // transaction.setCustomAnimations(R.anim.slide_in_left,R.anim.slide_out_right);
        switch (index) {
            case 1:
                if (marketingFragment == null) {
                    // 如果sanBiaoFragment为空，则创建一个并添加到界面上
                    marketingFragment = new MarketingFragment();
                    transaction.add(R.id.fl_central_document_activity,
                            marketingFragment);
                } else {
                    // 如果keepHaveFragment不为空，则直接将它显示出来
                    transaction.replace(R.id.fl_central_document_activity,
                            marketingFragment);

                }
                break;
            case 2:
                if (materielDownFragment == null) {
                    materielDownFragment = new MaterielDownFragment();
                    transaction.add(R.id.fl_central_document_activity,
                            materielDownFragment);
                } else {
                    transaction.replace(R.id.fl_central_document_activity,
                            materielDownFragment);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (marketingFragment != null) {
            transaction.remove(marketingFragment);
        }
        if (materielDownFragment != null) {
            transaction.remove(materielDownFragment);
        }
    }

}
